package com.lz.sports.ui

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.jeremyliao.liveeventbus.LiveEventBus
import com.lz.comm.BR
import com.lz.helper.idcard.IDCardHelper
import com.lz.helper.idcard.IDCardInfo
import com.lz.library.base.BaseActivity
import com.lz.library.extension.APP
import com.lz.library.extension.click
import com.lz.sports.R
import com.lz.sports.adapter.QueryAdapter
import com.lz.sports.bean.CommonModel
import com.lz.sports.core.ProjectManager
import com.lz.sports.core.SoundHelper
import com.lz.sports.databinding.ActQueryBinding
import com.lz.sports.ui.checkmodel.FaceIdentifyDialog
import com.lz.sports.ui.checkmodel.FeatureResult
import com.lz.sports.vm.QueryViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

//数据查询
class QueryActivity : BaseActivity<QueryViewModel, ActQueryBinding>(),
    IDCardHelper.IDCardCallback {

    private val adapter by lazy { QueryAdapter() }

    //身份证扫描
    private val idCardHelper by lazy { IDCardHelper(APP, true, this) }

    private var faceDialog: FaceIdentifyDialog? = null

    private val soundHelper by lazy { SoundHelper() }

    override fun getLayoutRes(): Int = R.layout.act_query

    override fun getVMVariableId(): Int = BR.vm

    override fun initObserve() {
        viewModel.examineeList.observe(this) {
            adapter.setNewData(it)
            binding.rvList.scrollToPosition(0)
        }

        viewModel.examineeAndCardInfo.observe(this) {
            faceDialog = FaceIdentifyDialog.show(this@QueryActivity, it.examinee,
                it.idCardInfo, viewModel.queryType, ProjectManager.personThreshold(), ProjectManager.faceThreshold())
        }

        LiveEventBus.get("FaceIdentifyDialog", FeatureResult::class.java).observe(this) { featureResult ->
            viewModel.searchExamineeResult(featureResult.examinee)
        }
    }

    override fun initData(savedState: Bundle?) {
        soundHelper.init(intArrayOf(com.lz.helper.R.raw.idcard_read, com.lz.helper.R.raw.idcard_success))
        binding.rvList.adapter = adapter
        lifecycleScope.launch {
            arrayOf(binding.ivClear, binding.flSearch, binding.tvAll).click().collect {
                when (it.id) {
                    R.id.iv_clear -> { binding.etInput.setText(""); viewModel.clearSearch() }
                    R.id.fl_search -> viewModel.search()
                    R.id.tv_all -> viewModel.selectAll()
                }
            }
        }

        idCardHelper.start()
    }

    override fun onResume() {
        super.onResume()
        idCardHelper.restart()
        lifecycleScope.launch {
            delay(500)
            soundHelper.play(0)
        }
    }

    override fun onPause() {
        super.onPause()
        idCardHelper.pause()
    }

    override fun onDestroy() {
        super.onDestroy()
        idCardHelper.quit()
        soundHelper.release()
    }

    override fun onCardResult(idCardInfo: IDCardInfo) {
        lifecycleScope.launch {
            soundHelper.play(1)
        }
        if (viewModel.queryType == CommonModel.ID_CARD_FACE_EXAM) {//人脸识别
            if (faceDialog?.dialog?.isShowing == true) return
            viewModel.examineeFace(idCardInfo)
        } else {//身份证方式
            viewModel.searchResult(idCardInfo)
        }
    }

    override fun onCardError(err: Int) {
    }
}